package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.BrandMapper;

import com.bwie.pojo.Brand;
import com.bwie.pojo.CategoryBrand;
import com.bwie.service.BrandService;

import com.bwie.utils.PageResult;
import com.bwie.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: day0423-pxx
 * @ClassName BrandServiceImpl
 * @description:
 * @author: 段阳
 * @create: 2024-04-23 20:06
 **/
@Service
@Transactional
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    @Autowired
    private BrandMapper brandMapper;






    @Override
    public PageResult pageList(Long page, Long rows, String key, String sortBy, Boolean desc) {
        QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isBlank(key)){
            queryWrapper.lambda().like(Brand::getName,key);
        }
        Page<Brand> brandPage = new Page<>(page,rows);
        if (StringUtils.isNotEmpty(sortBy)){
            brandPage.addOrder(desc? OrderItem.desc(sortBy):OrderItem.asc(sortBy));
        }
        Page<Brand> page1 = brandMapper.selectPage(brandPage, queryWrapper);
        if (page1.getTotal()==0){
            return new PageResult<Brand>();
        }
        PageResult<Brand> pageResult = new PageResult<>(page1.getTotal(), page1.getPages(), page1.getRecords());
        return pageResult;
    }

    @Override
    public R addNewBrand(Brand brand, List<Long> cids) {
        try {
            brandMapper.insert(brand);
            for (Long cid : cids) {
                brandMapper.addNewBrand(cid,brand.getId());
            }
            return R.success("success");
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return R.error(500,"error");
    }

    @Override
    public List<CategoryBrand> selectType(Long id) {
        return brandMapper.selectType(id);
    }

    @Override
    public R UPBrand(Brand brand, List<Long> cids) {
        try {
            brandMapper.updateById(brand);
           Integer i =  brandMapper.deleteCategory(brand.getId());
            for (Long cid : cids) {
                brandMapper.addNewBrand(cid,brand.getId());
            }
            return R.success("success");
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return R.error(500,"error");
    }

    @Override
    public R delBrand(Long id) {
        try {
            Brand brand = brandMapper.selectById(id);
            if (brand == null){
                return null;
            }
            brandMapper.deleteById(id);
//            R r = iPhotoService.delPhoto(brand);
            Integer i =  brandMapper.deleteCategory(id);
//            String s = r.toString();
//            if (s.contains("成功")){
//                return R.success("删除成功！");
//            }
            return R.success("删除成功！");
        } catch (RuntimeException e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        }
        return R.error(500,"删除失败！");
    }

    @Override
    public R getBrandInfo(Long id) {
        List<CategoryBrand> list = brandMapper.selectCategory(id);
        ArrayList<Brand> brands = new ArrayList<>();
        for (CategoryBrand categoryBrand : list) {
            Long brandId = categoryBrand.getBrandId();
            Brand brand = brandMapper.selectById(brandId);
            brands.add(brand);
        }
        return R.success("查询成功",brands);
    }

    @Override
    public Brand queryBrandNameByBrandId(Long id) {
        return brandMapper.selectById(id);
    }
}